package learnjdk.collector;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Created by alex on 2017/7/15.
 */
public class Main {

    public static void main(String[] args) {
//        System.out.println(partitionPrime(10));
        System.out.println(partitionPrime2(10));
    }

    // 普通方式
    private static Map<Boolean, List<Integer>> partitionPrime(int n) {
        return IntStream
                .rangeClosed(2, n)
                .boxed().collect(Collectors.partitioningBy(c -> isPrime(c)));
    }

    // 自建collector
    private static Map<Boolean, List<Integer>> partitionPrime2(int n) {
        Stream<Integer> stream = IntStream
                .rangeClosed(2, n)
                .boxed();
        return stream.collect(new PrimeNumbersCollector());
    }

    private static boolean isPrime(int c) {
        int cr = (int) Math.sqrt(c);
        return IntStream.rangeClosed(2, cr).noneMatch(i -> c % i == 0);
    }



    private static boolean isPrime(List<Integer> primes, int c) {
        return primes.stream().noneMatch(i -> c % i == 0);
    }



}
